package com.cc.moxingvoice.ui.voice;

import android.databinding.DataBindingUtil;
import android.view.MotionEvent;
import android.view.View;

import com.cc.moxingvoice.R;
import com.cc.moxingvoice.base.BaseActivity;
import com.cc.moxingvoice.databinding.ActivityVoiceBinding;
import com.cc.moxingvoice.lock.AppBarLock;
import com.cc.moxingvoice.lock.VoiceLock;

/**
 * 创建日期：2018/2/24 on 9:40
 * 描述：
 * 作者：郭士超
 * QQ：1169380200
 */

public class VoiceActivity extends BaseActivity {

    private ActivityVoiceBinding mBinding;
    private VoiceLock mLock;
    private AppBarLock appBarLock;

    @Override
    protected void initBinding() {
        // 数据绑定操作，可以套用代码
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_voice);
        mLock = new VoiceLock(this, mBinding, mBundle);
        mBinding.setLock(mLock);
        if (mBundle.getBoolean("assets")) {
            appBarLock = new AppBarLock(this, mLock, R.string.app_name, true);
        }else {
            appBarLock = new AppBarLock(this, mLock, getString(R.string.app_name),
                    R.drawable.back, 0, "", getString(R.string.manage), AppBarLock.BACK, AppBarLock.VOICE_RIGHT);
        }
        mBinding.appBar.setAppBarLock(appBarLock);
    }

    @Override
    protected void init() {
        String title = mBundle.getString("title");
        appBarLock.setTitle(title);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (notInView(mBinding.llAddType, ev) && notInView(mBinding.tvAdd, ev)) {
                    mLock.hideStartType();
                }
                break;
            case MotionEvent.ACTION_MOVE:
//                mLock.hideStartType();
                break;
            case MotionEvent.ACTION_UP:
//                mLock.hideStartType();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean notInView(View v, MotionEvent event) {
        int[] l = {0, 0};
        v.getLocationInWindow(l);
        int left = l[0],
                top = l[1],
                bottom = top + v.getHeight(),
                right = left + v.getWidth();
        if (event.getX() > left && event.getX() < right
                && event.getY() > top && event.getY() < bottom) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    protected void onResume() {
        mLock.refreshList();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mLock.onDestroy();
        super.onDestroy();
    }
}
